package mystruts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

	File pic1;
	String pic1ContentType;
	String pic1FileName;
	
	public File getPic1() {
		return pic1;
	}

	public void setPic1(File pic1) {
		this.pic1 = pic1;
	}

	
	public String getPic1ContentType() {
		return pic1ContentType;
	}

	public void setPic1ContentType(String pic1ContentType) {
		this.pic1ContentType = pic1ContentType;
	}

	public String getPic1FileName() {
		return pic1FileName;
	}

	public void setPic1FileName(String pic1FileName) {
		this.pic1FileName = pic1FileName;
	}

	public String execute(){
		
		return "input";
	}
	
	public String upload() throws Exception {
		this.LOG.info("pic1:"+pic1);
		this.LOG.info("pic1ContentType:"+pic1ContentType);
		this.LOG.info("pic1FileName:"+pic1FileName);
		String saveDir=
				ServletActionContext.getServletContext().getRealPath("upload")
				+"\\"+pic1FileName; 
		this.LOG.info("saveDir:"+saveDir);
		String source=pic1.getAbsolutePath();
		this.LOG.info("source:"+source);
		this.copyFile(source, saveDir);
		return "list";
	}
	public  void copyFile(String srFile, String dtFile){
		  try{
		       FileChannel srcChannel = new FileInputStream(srFile).getChannel();
		       FileChannel dstChannel = new FileOutputStream(dtFile).getChannel();
		       dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
		       srcChannel.close();
		       dstChannel.close();
		  } catch (IOException e) {
		      e.printStackTrace();    
		  }
		}
}
